package dao

import (
	"enotify/internal/xys/notify/model"
	"errors"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"strings"
)
type MySql struct {
	UserName string
	Password string
	Addr string
	Db string
}

func NewMySql(userName,passwd,addr,db string) *MySql{
	return &MySql{
		UserName: userName,
		Password: passwd,
		Addr:     addr,
		Db:       db,
	}
}
func (d *Dao) AddLog(log model.Log) (affected int64,err error)  {
	affected, err = d.engine.Insert(&log)
	return
}
func (d *Dao) UpdateLog(id int64, log model.Log) (affected int64,err error)  {
	affected,err = d.engine.ID(id).Update(log)
	return
}
func (d *Dao) GetLog(id int64) (log model.Log,find bool,err error) {
	find,err = d.engine.Where("fid=?",id).Get(&log)
	return
}
func (d *Dao) FingLogByFilter(filter Filter) (logs []model.Log,err error){
	if filter.where == nil || filter.args == nil {
		return nil, errors.New("where or filter is nil")
	}
	wstring := strings.Join(filter.where," ")
	session := d.engine.Where(wstring,filter.args...)
	if filter.cols != nil {
		session = session.Cols(filter.cols...)
	}
	err = session.Find(&logs)
	return
}
func (d *Dao) GetEvent(id int64) (e model.Event,find bool,err error){
	find,err = d.engine.Where("fid=?",id).Get(&e)
	return
}
func (d *Dao) GetWxTemplate(id int) (t model.WxTemplate,find bool,err error){
	find,err = d.engine.Table("t_touchup_wx_template").Where("fid=?",id).Cols("ftemplate_id","fcontent").Get(&t)
	return
}
func NewFilter(prefix string) *Filter {
	return &Filter{prefix: prefix}
}
type Filter struct {
	prefix string
	cols []string
	where []string
	args []interface{}
}
func (f *Filter) GetWhere() []string {
	return f.where
}
func (f *Filter) GetArgs() []interface{} {
	return f.args
}
func (f *Filter) Cols(keys ...string){
	for _,key := range keys {
		f.cols = append(f.cols,fmt.Sprintf("%s%s",f.prefix,key))
	}
}
func (f *Filter) Eq(key string,arg interface{},ops ...string) {
	op := f.getLinkOp(ops)
	f.args = append(f.args, arg)
	f.where =append(f.where,fmt.Sprintf("%s%s%s=?",op,f.prefix,key))
}
func (f *Filter) GT(key string,arg interface{},ops ...string) {
	op := f.getLinkOp(ops)
	f.args = append(f.args, arg)
	f.where =append(f.where,fmt.Sprintf("%s%s%s>?",op,f.prefix,key))
}
func (f *Filter) EGT(key string,arg interface{},ops ...string) {
	op := f.getLinkOp(ops)
	f.args = append(f.args, arg)
	f.where =append(f.where,fmt.Sprintf("%s%s%s>=?",op,f.prefix,key))
}
func (f *Filter) LT(key string,arg interface{},ops ...string) {
	op := f.getLinkOp(ops)
	f.args = append(f.args, arg)
	f.where =append(f.where,fmt.Sprintf("%s%s%s<?",op,f.prefix,key))
}
func (f *Filter) ELT(key string,arg interface{},ops ...string) {
	op := f.getLinkOp(ops)
	f.args = append(f.args, arg)
	f.where =append(f.where,fmt.Sprintf("%s%s%s<=?",op,f.prefix,key))
}
func(f *Filter) In(key string,args []interface{},ops ...string)  {

	op := f.getLinkOp(ops)
	if len(args) == 0 {
		return
	}
	strs := make([]string,len(args))
	for i := 0; i<len(args);i++ {
		strs[i] = "?"
	}
	f.where = append(f.where,fmt.Sprintf("%s%s%s IN(%s)",op,f.prefix,key,strings.Join(strs,",")))
	f.args = append(f.args,args...)
}
func (f *Filter) getLinkOp(ops []string) string  {
	if len(f.where) == 0 {
		return ""
	}
	var op string
	if ops != nil {
		op = ops[0]
	}
	if op != ""{
		return fmt.Sprintf(" %s ",op)
	}
	op = " AND "
	return " AND "
}

